# -*- coding: utf-8 -*-
import scrapy
from abc import ABCMeta, abstractmethod


class BaseItem(metaclass=ABCMeta):
    """
    基础的每个Item都应该实现的接口
    """

    field_list = []  # 字段名

    @abstractmethod
    def clean_data(self):
        """
        对于原始提取字段进行清理
        :return:
        """
        pass

    @staticmethod
    @abstractmethod
    def help_fields(fields: list):
        """
        帮助生成field定义字段代码。
        :param fields:
        :return:
        """
        pass


class MongoDBItem(BaseItem):
    """
    数据存取至 MongoDB 应该实现的接口
    """

    @abstractmethod
    def getCollection(self):
        """
        :return: 返回要操作的Collection名字
        """
        pass

    @abstractmethod
    def getId(self):
        """
        :return: 返回能作为Collection查询条件，主要用来去重。
        """
        pass


class QuoteItem(scrapy.Item, MongoDBItem):
    field_list = ['text', 'author', 'tags', 'url_object_id']
    table_name = 'sample_saying'

    text = scrapy.Field()
    author = scrapy.Field()
    tags = scrapy.Field()
    url_object_id = scrapy.Field()

    def getCollection(self):
        return self.table_name

    def getId(self):
        return {'url_object_id': self["url_object_id"]}

    def help_fields(self):
        for field in self.field_list:
            print(field, "= scrapy.Field()")

    def clean_data(self):
        pass
